package com.hu.zhcc.shiro.controller.Reponsitory;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.Reponsitory.RDietary;
import com.hu.zhcc.shiro.service.Responsitory.DietaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 膳食库
 */
@RestController
public class DietaryController {

    @Autowired
    private DietaryService dietaryService;

    /***
     * 分页及条件查询
     * @param offset
     * @param limit
     * @param diename
     * @return
     */
    @GetMapping("dietaryAll")
    private Page<RDietary> dietaryAll(String offset, String limit, String diename) {
        RDietary dietary = new RDietary(diename);
        Map map = new HashMap();
        map.put("offset", offset);
        map.put("limit", limit);
        return dietaryService.selectTaryAll(map, dietary);
    }

    /***
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("deleteTaryById{id}")
    private int deleteTaryById(@PathVariable("id") int id) {
        return dietaryService.deleteByPrimaryKey(id);
    }

    /***
     * 添加
     * @param dietarys
     * @return
     */
    @PostMapping("addDietarys")
    private int saveDietray(@RequestBody RDietary dietarys) {
        dietarys.setAryEnergy(dietarys.getAryKilocal() / 10);
        dietaryService.insertSelective(dietarys);
        Integer id = dietarys.getId();
        if (id < 10) {
            dietarys.setAryCoding("SS00" + dietarys.getId());
            return dietaryService.updateByPrimaryKeySelective(dietarys);
        } else if (id >= 10) {
            dietarys.setAryCoding("SS0" + dietarys.getId());
            return dietaryService.updateByPrimaryKeySelective(dietarys);
        } else {
            dietarys.setAryCoding("SS" + dietarys.getId());
            return dietaryService.updateByPrimaryKeySelective(dietarys);
        }
    }

    /***
     * 修改前查询
     * @param id
     * @return
     */
    @PostMapping("beforetaryByid{id}")
    private RDietary beforetaryByid(@PathVariable("id") int id) {
        return dietaryService.taryBeforeUpdateById(id);
    }

    /***
     * 修改
     * @param tary
     * @return
     */
    @PutMapping("dietaryUpdate")
    private int dietaryUpdate(@RequestBody RDietary tary) {
        return dietaryService.updateByPrimaryKeySelective(tary);
    }


    /***
     * 查询所以食品名称
     */
    @GetMapping("selectDietaryNameList")
    private ResponseEntity<List<RDietary>> selectDietaryNameList(){
        List<RDietary> dietaryListNameAll = dietaryService.findDietaryListNameAll();
        return ResponseEntity.ok(dietaryListNameAll);
    }
}
